using System;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Collections;
using System.Drawing;
using System.Workflow.ComponentModel;
using System.Workflow.ComponentModel.Design;
using System.Workflow.ComponentModel.Compiler;
using System.Workflow.ComponentModel.Serialization;
using System.Workflow.Runtime;
using System.Workflow.Activities;
using System.Workflow.Activities.Rules;


using System.Data.Linq;
using System.Linq;

using DataLayer;

namespace BusinessLayer.Activities
{
   public partial class Update_WidgetInstanceProperty : System.Workflow.ComponentModel.Activity
   {
      public Update_WidgetInstanceProperty()
      {
         //InitializeComponent();
      }

      private static DependencyProperty WidgetInstanceIdProperty = DependencyProperty.Register("WidgetInstanceId", typeof(int), typeof(Update_WidgetInstanceProperty));
      public int WidgetInstanceId
      {
         get { return (int)base.GetValue(WidgetInstanceIdProperty); }
         set { base.SetValue(WidgetInstanceIdProperty, value); }
      }

      private static DependencyProperty wPropertyProperty = DependencyProperty.Register("wProperty", typeof(string), typeof(Update_WidgetInstanceProperty));
      public string wProperty
      {
         get { return (string)base.GetValue(wPropertyProperty); }
         set { base.SetValue(wPropertyProperty, value); }
      }

      private static DependencyProperty wValueProperty = DependencyProperty.Register("wValue", typeof(string), typeof(Update_WidgetInstanceProperty));
      public string wValue
      {
         get { return (string)base.GetValue(wValueProperty); }
         set { base.SetValue(wValueProperty, value); }
      }

      protected override ActivityExecutionStatus Execute(ActivityExecutionContext executionContext)
      {
         using (var db = DatabaseHelper.GetDashboardData())
         {
            var w = db.TabWidgets.Single(p => p.Id == WidgetInstanceId);

            switch (wProperty.ToLower())
            {
               case "title":
                  w.Title = wValue;
                  break;
               case "expanded":
                  w.Expanded = wValue.ToLower() == "true";
                  break;
               case "state":
                  w.State = wValue;
                  break;
               case "height":
                  //use int 16 so that max value is set to 32k even though db allows more than that
                  try
                  {
                     w.Height = Convert.ToInt16(wValue);
                  }
                  catch
                  {
                     w.Height = 0;
                  }
                  break;
            }

            db.SubmitChanges();
         }
         return ActivityExecutionStatus.Closed;
      }
   }
}